// =============================================================================
// Functions
//
// Table of contents:
// 1. Get breakpoint Names list
// 2. Get breakpoint Values list
// 3. Get breakpoint Groups list with min and max values
// 4. Get the Respond-to breakpoint
// 5. Get size (px or em)
// 6. Yepnope
//
// =============================================================================

// =============================================================================
// 1. Get breakpoint Names list
// =============================================================================

@function getBreakpointNames($breakpoints) {
	$names: ();
	@for $i from 1 through length($breakpoints) {
		@if type-of(nth(nth($breakpoints,$i),1)) == string {
			$names: join($names, nth(nth($breakpoints,$i),1));
		} @else {
			@warn '[ERROR] "' + nth(nth($breakpoints,$i),1) + '" is not a valid breakipoint name';
		}
	}
	@return $names;
}

// =============================================================================
// 2. Get breakpoint Values list
// =============================================================================

@function getBreakpointValues($breakpoints) {
	$values: ();
	@for $i from 1 through length($breakpoints) {
		@if unit(nth(nth($breakpoints,$i),2)) == px  {
			$values: join($values, nth(nth($breakpoints,$i),2));
		} @else {
			@warn '[ERROR] "' + nth(nth($breakpoints,$i),2) + '" is not a valid breakipoint value';
		}
	}
	@return $values;
}

// =============================================================================
// 3. Get breakpoint Groups list with min and max values
// =============================================================================

@function createBreakpointGroups($breakpoint-names, $breakpoint-values) {
	$captured-groups: ();
	$new-group: ();
	$groups: ();

	// Create a list with all the captured groups breakpoint names and indexes
	@for $i from 1 through length($breakpoint-names) {
		$group: regex(nth($breakpoint-names,$i), '([a-z]+)?\-');
		@if $group != '' {
			$group: join($group, $i, space);
			$captured-groups: append($captured-groups, $group, comma);
		}
	}

	// Save a list of groups with min and max values
	// eg: foo 100px 200px, bar 200px 300px;
	@for $i from 1 through length($captured-groups) {

		$current-group: nth($captured-groups, $i);
		$current-group-name: nth($current-group,1);
		$current-group-index: nth($current-group,2);

		// Initial group
		@if $i == 1 {
			$new-group: $current-group-name;
			$new-group: append($new-group, nth($breakpoint-values, $current-group-index), space);
		// New and last groups
		} @else if $current-group-name != nth($new-group, 1) or $i == length($captured-groups) {

			// Find the current breakpoint value (max-width for the group)
			$current-breakpoint-value: nth($breakpoint-values, ($current-group-index));

			// If it's the last group and there's a higer breakpoint
			@if $i == length($captured-groups) and $current-group-index < length($breakpoint-values) {
				// Find the current breakpoint value (max-width for the group)
				$current-breakpoint-value: nth($breakpoint-values, ($current-group-index + 1));
			}

			// The current group is ready
			$new-group: append($new-group, $current-breakpoint-value, space);
			// Save the final group in the groups list
			$groups: append($groups, $new-group, comma);
			// Start a new group adding its name and min value
			$new-group: $current-group-name;
			$new-group: append($new-group, nth($breakpoint-values, $current-group-index), space);
		}
	}
	@return $groups;
}

// =============================================================================
// 4. Get the Respond-to breakpoint
// =============================================================================

@function getBreakpointInfo($breakpoint) {

	@if $breakpoint == ie {
		@return ie;
	} @else if $breakpoint == retina {
		@return retina;
	} @else {

		// capture the extension (only, and-up or and-below)
		$extension: regex($breakpoint, '((and-)?[a-z]+)$');

		// capture the breakpoint name
		$root: regex($breakpoint, '(.*?)-'+ $extension + '$');

		// Check if it's a normal breakpoint
		@if yepnope($breakpoint-names, $root) {
			$group: false;
			$groupid: false;
			$br: join($root, $extension, space);
			$br: join($br, $group, space);
			$br: join($br, $groupid, space);
			@return $br;
		}

		// Check if it's a group
		@for $i from 1 through length($breakpoint-groups) {
			@if $root == nth(nth($breakpoint-groups, $i),1) {
				$group: true;
				$groupid: $i;
				$br: join($root, $extension, space);
				$br: join($br, $group, space);
				$br: join($br, $groupid, space);
				@return $br;
			}
		}

		@warn '[ERROR] "' + $breakpoint + '"" is not a valid parameter. Please, check the breakpoint name and also make sure you used -only, -and-up, or -and-below]';
		@return 'error';
	}
}

// =============================================================================
// 5. Get size
// Returns the breakpoint value in pixels or ems.
// =============================================================================

@function getSize($value, $context: $responder-ems-context) {
	@if $responder-use-ems == true {
		@return $value / $context +em;
	} @else {
		@return $value;
	}
}

// =============================================================================
// 6. Yepnope
// Take a $list, return true if any $args are present.
// =============================================================================

@function yepnope($list, $args) {
  $list: compact($list);
  $check: false;
  @each $arg in $args {
    $check: if(index($list,$arg), true, $check);
  }
  @return $check;
}